//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Eas.WebCrawler.Model;

namespace Eas.WebCrawler.Test.Model {

    /// <summary>
    /// Summary description for ChangeRequestQueueTest
    /// </summary>
    [TestClass]
    public class ChangeRequestQueueTest {
        Eas.WebCrawler.Model.Core.ChangeQueue _Queue;

        public ChangeRequestQueueTest() {
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //

        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize() {
            _Queue = new Eas.WebCrawler.Model.Core.ChangeQueue();
        }

        //
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup() {
            _Queue = null;
        }

        //
        #endregion

        [TestMethod]
        /// <summary>Executes the TestSimpleQueue test.</summary>
        public void TestSimpleQueue() {
            _Queue.EnQueueRequest(Eas.WebCrawler.Interfaces.WorkerState.Working);
            _Queue.EnQueueRequest(Eas.WebCrawler.Interfaces.WorkerState.Paused);
            _Queue.EnQueueRequest(Eas.WebCrawler.Interfaces.WorkerState.Working);
            _Queue.EnQueueRequest(Eas.WebCrawler.Interfaces.WorkerState.Stopped);

            CheckQueue(Eas.WebCrawler.Interfaces.WorkerState.Working);
            CheckQueue(Eas.WebCrawler.Interfaces.WorkerState.Paused);
            CheckQueue(Eas.WebCrawler.Interfaces.WorkerState.Working);
            CheckQueue(Eas.WebCrawler.Interfaces.WorkerState.Stopped);
        }

        [TestMethod]
        /// <summary>Executes the CheckEmptyQueue test.</summary>
        public void CheckEmptyQueue() {
            CheckQueue(null);
        }

        private void CheckQueue(Eas.WebCrawler.Interfaces.WorkerState? newState) {
            if (newState.HasValue) {
                Eas.WebCrawler.Interfaces.WorkerState? valueToCheck = _Queue.DeQueueRequest();
                Assert.IsTrue(valueToCheck.HasValue);
                Assert.AreEqual<Eas.WebCrawler.Interfaces.WorkerState>(newState.Value, valueToCheck.Value);
            }
            else
                Assert.IsFalse(_Queue.DeQueueRequest().HasValue);
        }

    }
}
